<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Entity_Tag_Collection extends Core_Db_Collection {

	public function addPostFilter($id) {
		$this->getSelect()
			->columns(array('tag.*'))
			->join('post_tag pt', 'pt.tag_id = tag.id')
			->where("pt.post_id = {$id}");
		return $this;
	}
	
	public function getTagString() {
		$tags = array();
		foreach($this as $tag)
			$tags[] = $tag->getName();
		return implode(', ', $tags);
	}
	
	public function addPostCount() {
		$this->getSelect()
			->columns(array('tag.*', 'COUNT(post_id) AS post_count'))
			->joinLeft('post_tag pt', 'pt.tag_id = tag.id')
			->group('tag.id');
		return $this;
	}
}